﻿using System;
using System.IO;
using System.Timers;
using System.Windows.Forms;

namespace Scenario_Editor.Tools
{
    internal class AutoSaver
    {
        #region Public Fields

        public EventHandler NewLogMessage;

        #endregion Public Fields

        #region Private Fields

        private int CurrentFileID = 1;
        private Engine.Engine engine;
        private ScenarioFile sceneryFile;
        private System.Timers.Timer timer;

        #endregion Private Fields

        #region Public Constructors

        public AutoSaver(Engine.Engine engine, int intervalMinutes, ScenarioFile sceneryFile)
        {
            this.engine = engine;
            timer = new System.Timers.Timer();
            SetNewTime(intervalMinutes, null);
            //timer.Interval = 1000 * 60 * intervalMinutes;
            timer.Elapsed += timer_Elapsed;
            //timer.Enabled = true;
            this.sceneryFile = sceneryFile;
        }

        #endregion Public Constructors

        #region Public Properties

        public bool Enable
        {
            get { return this.timer.Enabled; }
            set { this.timer.Enabled = value; }
        }

        #endregion Public Properties

        #region Internal Methods

        internal void SetNewTime(object sender, EventArgs e)
        {
            int timeInMilliseconds = ((int)sender) * 60000;
            if (timeInMilliseconds > 0)
            {
                this.timer.Interval = timeInMilliseconds;
                this.timer.Enabled = true;
            }
            else
                this.timer.Enabled = false;
        }

        #endregion Internal Methods

        #region Private Methods

        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            timer.Enabled = false;
            string directory = Application.StartupPath + "\\AutoBackup\\";
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);
            string filename = directory + "Autobackup" + CurrentFileID.ToString().PadLeft(2, '0') + ".WRScenario";
            try
            {
                if (sceneryFile != null)
                {
                    CurrentFileID++;
                    if (CurrentFileID >= 100)
                        CurrentFileID = 1;

                    sceneryFile.SaveToFile(filename);
                    this.engine.Grafic.CreateScreenshot(directory + "Autobackup" + CurrentFileID.ToString().PadLeft(2, '0') + ".jpg", SharpDX.Direct3D11.ImageFileFormat.Jpg);
                    NewLogMessage(DateTime.Now.ToShortTimeString() + ": " + Path.GetFileName(filename), null);
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show("Can´t save scenery autobackup: " + filename + "\n\n" + ex.Message);
            }
            timer.Enabled = true;
        }

        #endregion Private Methods
    }
}